package com.gray.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;

import net.coobird.thumbnailator.Thumbnails;
public class ImageUtil {
	
	
	
	@Test
	public void imageWaterMart() throws IOException{
//		System.out.println(System.getProperty("os.name").equals("Windows 7"));
		
//		imageAdd(new FileInputStream(new File("D:\\iocn1.jpg")),"aaaas\\as","ssaa.jpg");
		
//		Thumbnails.of(new File("E:\\QQ截图20170918112419.png")).size(50, 50).keepAspectRatio(false).toFile(new File("E:\\0170918112419.png"));
	}
	
	public static void thumBnails(int size,String path) throws IOException{
		Thumbnails.of(new File(path)).size(size, size).toFile(new File(path));;
	}
	
	 
	/**
	 * 添加图片 根据路径添加，修改原文件名字
	 * @param is 图片流
	 * @param oldFilename 原文件名
	 * @param des 目录
	 * @return 相对路劲/新文件名
	 * @throws IOException 
	 */
	
	public static String  imageAdd(InputStream is,String des,String oldFilename){
		//windows 系统
		String fileName = "";
		
			Date date = new Date();
			int byteread = 0;
			fileName =date.getTime()+ "."+org.apache.commons.lang.StringUtils.substringAfterLast(oldFilename, ".");
			File newFile = new File(BasePathUtil.getImgBasePath()+des+ System.getProperty("file.separator")+fileName);
			File fileDir = new File(BasePathUtil.getImgBasePath()+des);
			
			FileOutputStream fos = null;
			
			try{
				if(!fileDir.exists()){
					fileDir.mkdirs();
				}
				if(!newFile.exists()){
					newFile.createNewFile();
				}
				byte[] bye = new byte[1048576*10]; //最大10M 否者回出差错 
				 fos = new FileOutputStream(newFile);
				while((byteread=is.read(bye))!=-1){
				is.read(bye);
				fos.write(bye,0,byteread);
				fos.flush();
				}
			}catch(Exception e){
				
			}finally{
				try {
					if(fos!=null){
					fos.close();}
					if(is!=null){
					is.close();}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
				
		
		return des+ System.getProperty("file.separator")+fileName;
		
	}
	
	/**
	 * 文件名是否属于图片类型 jpg png gmp gif
	 * @param fileName
	 * @return
	 */
	public static boolean isImageFile(String fileName){
		if(fileName.endsWith(".jpg") 
				|| fileName.endsWith(".png") 
				|| fileName.endsWith(".bmp")
				||fileName.endsWith("gif")){
			
			return true;
		}
		
		
		
		return false;
	}
	
	
	
	/**
	 * 删除图片
	 * @param imgPath 图片所在相对路径
	 */
	public static void imageDel(String imgPath){
		File file =null;
		//windows系统
		if(System.getProperty("os.name").equals("Windows 7")){
			file = new File (BasePathUtil.getImgBasePath()+imgPath);
			System.out.println("删除图片的路径是："+BasePathUtil.getImgBasePath()+imgPath);
			if(file.exists()){
				
				file.delete();
			}
		}else{
			file =new File(BasePathUtil.getImgBasePath()+imgPath);
			System.out.println("删除图片的路径是："+BasePathUtil.getImgBasePath()+imgPath);
			if(file.exists()){
				file.delete();
			}
		}
	}
	
}
